package com.linkvin.ec.hcs.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.linkvin.ec.hcs.dao.model.mdm.bo.WarehouseBo;
import com.linkvin.ec.hcs.service.mdm.WarehouseService;
import com.linkvin.ec.hcs.service.util.enums.WarehouseStyleEnum;
import com.linkvin.ec.hcs.service.util.enums.WarehouseTypeEnum;
import com.linkvin.ec.hcs.util.EnumUtil;
import com.linkvin.ec.hcs.vo.EnumVO;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.WarehouseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/warehouse", produces = MediaType.APPLICATION_JSON_VALUE)
public class WarehouseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WarehouseController.class);

    @Autowired
    private WarehouseService warehouseService;



    @PostMapping(value = "/queryPage")
    public RestVO<Page<WarehouseVO>> queryWarehouseList(@RequestBody Page<WarehouseBo> page){
        RestVO<Page<WarehouseVO>> result = new RestVO<>(0,"success");
        LOGGER.info("WarehouseController.queryWarehouseList params : {}", page);

        try{
            Page<WarehouseVO> pageWarehouseVO = new Page<>();

            page = warehouseService.queryPageWarehouse(page);
            BeanUtils.copyProperties(page, pageWarehouseVO);

            List<WarehouseVO> listWarehouseVO = Lists.newArrayList();
            List<WarehouseBo> listWarehouseBo = page.getRecords();

            listWarehouseBo.forEach(warehouseBo -> {
                WarehouseVO warehouseVO = new WarehouseVO();
                BeanUtils.copyProperties(warehouseBo,warehouseVO);
                warehouseVO.setType(WarehouseTypeEnum.getByValue(warehouseVO.getType()).getText());
                warehouseVO.setStyle(WarehouseStyleEnum.getByValue(warehouseVO.getStyle()).getText());
                listWarehouseVO.add(warehouseVO);
            });

            pageWarehouseVO.setRecords(listWarehouseVO);
            result.setData(pageWarehouseVO);

        }catch(Exception ex)
        {
            LOGGER.error("WarehouseController.queryWarehouseList ERROR Message : {} ", ex.getMessage());
            result.setCode(30004);
            result.setMessage("仓库查询错误");
        }

        return result;
    }

    @GetMapping(value = "/queryWhType")
    public RestVO<List<EnumVO>> queryWhTypeVO(){
        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = WarehouseTypeEnum.lookup();
        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);
        result.setData(lists);
        return  result;
    }

    @GetMapping(value = "/queryWhStyle")
    public RestVO<List<EnumVO>> queryWhStyleVO(){
        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = WarehouseStyleEnum.lookup();

        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);
        result.setData(lists);
        return  result;
    }

    @PostMapping(value = "/create")
    public RestVO<WarehouseVO> create(@RequestBody WarehouseBo warehouseBo){

        RestVO<WarehouseVO> result = new RestVO<>(0,"success");

        LOGGER.info("WarehouseController.create params : {}", warehouseBo);

        try {

            WarehouseVO warehouseVO =new WarehouseVO();
            warehouseBo = warehouseService.addWarehouse(warehouseBo);
            BeanUtils.copyProperties(warehouseBo,warehouseVO);
            warehouseVO.setType(WarehouseTypeEnum.getByValue(warehouseVO.getType()).getText());
            warehouseVO.setStyle(WarehouseStyleEnum.getByValue(warehouseVO.getStyle()).getText());

            result.setData(warehouseVO);
        }catch (Exception e){
            LOGGER.error("DepartmentController.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<WarehouseVO> update(@RequestBody WarehouseBo warehouseBo){
        RestVO<WarehouseVO> result = new RestVO<>(0,"success");

        LOGGER.info("WarehouseController.update params : {}", warehouseBo);

        try {

            WarehouseVO warehouseVO =new WarehouseVO();
            warehouseBo = warehouseService.updateWarehouse(warehouseBo);
            BeanUtils.copyProperties(warehouseBo,warehouseVO);
            warehouseVO.setType(WarehouseTypeEnum.getByValue(warehouseVO.getType()).getText());
            warehouseVO.setStyle(WarehouseStyleEnum.getByValue(warehouseVO.getStyle()).getText());

            result.setData(warehouseVO);
        }catch (Exception e){
            LOGGER.error("WarehouseController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> deleteWarehouse(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("WarehouseController.deleteWarehouse params : {}", ids);

        try {
            int i =  warehouseService.deleteWarehouse(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("WarehouseController.deleteWarehouse ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    @GetMapping(value = "/listAll")
    public RestVO<List<WarehouseVO>> listAllWarehouse(){
        RestVO<List<WarehouseVO>> result = new RestVO<>(0,"success");
        LOGGER.info("WarehouseController.update params");

        try {
            List<WarehouseVO> listWarehouseVo = Lists.newArrayList();
            List<WarehouseBo> listWarehouseBo= warehouseService.listAllWarehouse();

            listWarehouseBo.forEach(warehouseBo -> {
                WarehouseVO warehouseVO = new WarehouseVO();
                BeanUtils.copyProperties(warehouseBo,warehouseVO);
                listWarehouseVo.add(warehouseVO);
            });
            result.setData(listWarehouseVo);
        }catch (Exception e){
            LOGGER.error("WarehouseController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }


}
